package com.zz.cc.owner.util;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class PinYin4j {
	
	
	public PinYin4j(){
	}
	
	public String makeStringByStringSet(Set<String> stringSet) {
		StringBuilder str = new StringBuilder();
		int i = 0;
		for (String s : stringSet) {
			if (i == stringSet.size() - 1) {
				str.append(s);
			} else {
				str.append(s + ",");
			}
			i++;
		}
		return str.toString().toLowerCase();
	}

	
	public Set<String> getPinyin(String src) {
			char[] srcChar;
			srcChar = src.toCharArray();

			//1:多少个汉字
			//2:每个汉字多少种读音
			String[][] temp = new String[src.length()][];
			for (int i = 0; i < srcChar.length; i++) {
				char c = srcChar[i];
				if (String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")) {
						String[] t = PinyinHelper.getUnformattedHanyuPinyinStringArray(c);
						temp[i] = new String[t.length];
						for(int j=0;j<t.length;j++){
							temp[i][j]=t[j].substring(0,1);
						}
				} else if (((int) c >= 65 && (int) c <= 90)
						|| ((int) c >= 97 && (int) c <= 122)||c>=48&&c<=57||c==42) {//a-zA-Z0-9*
					temp[i] = new String[] { String.valueOf(srcChar[i]) };
				} else {
					temp[i] = new String[] {"null!"};
				}
				
			}
			String[] pingyinArray = paiLie(temp);
			return array2Set(pingyinArray);//过滤重复的
	}
	
	private String[] paiLie(String[][] str){
		int max=1;
		for(int i=0;i<str.length;i++){
			max*=str[i].length;
		}
		String[] result=new String[max];
		for(int i = 0; i < max; i++){
	            String s = "";
	            int temp = 1;      
	            for(int j = 0; j < str.length; j++){
	                temp *= str[j].length;
	                s += str[j][i / (max / temp) % str[j].length];
	            }
	            result[i]=s;
	    }
		
		return result;
	}
	
	public static <T extends Object> Set<T> array2Set(T[] tArray) {   
        Set<T> tSet = new HashSet<T>(Arrays.asList(tArray));   
        return tSet;   
    } 
	
	public static void main(String[] args) {
		PinYin4j t=new PinYin4j();
		String str = "农业银行1234567890abcdefghijklmnopqrstuvwxyz*";
		System.out.println(t.makeStringByStringSet(t.getPinyin(str)));
	}

}
